package xin.clips.quartz.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import xin.clips.system.utils.ByteUtils;
import xin.clips.system.utils.LogUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

/**
 * @desc: 小车数据采集定时任务
 * @author: Zruiry
 * @date:
 */
@Component("carInfoTask")
public class CarInfoTask {

    private static final Logger logger = LoggerFactory.getLogger(CarInfoTask.class);

    private static final Integer PORT = 6667;

    /**
     * 采集小车数据
     */
    public void gather() {
        try {
            byte[] msg = new byte[100];
//            DatagramSocket udp = new DatagramSocket(PORT,InetAddress.getByName("192.168.2.99"));
            DatagramSocket udp = new DatagramSocket(PORT);
            DatagramPacket packet = new DatagramPacket(msg, 1024);
            udp.receive(packet);
            packet.getData();
            logger.info(msg.length + "接收到的报文{}", msg);
            byte[] data = Arrays.copyOfRange(msg, 12, 100);
            byte[] x = Arrays.copyOfRange(data, 8, 16);
            double xValue = ByteUtils.bytes2Double(x);
            System.out.println("robot_pose:x=" + xValue + " ");
            byte[] y = Arrays.copyOfRange(data, 16, 24);
            double yValue = ByteUtils.bytes2Double(y);
            System.out.println("robot_pose:y=" + yValue);
            byte[] theta = Arrays.copyOfRange(data, 24, 32);
            double thetaValue = ByteUtils.bytes2Double(theta);
            System.out.println("robot_pose:theta=" + thetaValue);
            byte[] fork_x = Arrays.copyOfRange(data, 32, 40);
            double fork_x_Value = ByteUtils.bytes2Double(fork_x);
            System.out.println("fork_pose:x=" + fork_x_Value + " ");
            byte[] fork_y = Arrays.copyOfRange(data, 40, 48);
            double fork_yValue = ByteUtils.bytes2Double(fork_y);
            System.out.println("fork_pose:y=" + fork_yValue);
            byte[] fork_y_theta = Arrays.copyOfRange(data, 48, 56);
            double fork_y_thetaValue = ByteUtils.bytes2Double(fork_y_theta);
            System.out.println("fork_pose:theta=" + fork_y_thetaValue);
            byte[] robot_forward_point_x = Arrays.copyOfRange(data, 56, 64);
            double robot_forward_point_x_Value = ByteUtils.bytes2Double(robot_forward_point_x);
            System.out.println("robot_forward_point:x=" + robot_forward_point_x_Value + " ");
            byte[] robot_forward_point_y = Arrays.copyOfRange(data, 64, 72);
            double robot_forward_point_y_Value = ByteUtils.bytes2Double(robot_forward_point_y);
            System.out.println("robot_forward_point:y=" + robot_forward_point_y_Value);
            byte[] robot_busy_flag = Arrays.copyOfRange(data, 72, 73);
            System.out.println("robot_busy_flag=" + robot_busy_flag[0]);
            byte[] avoid_state = Arrays.copyOfRange(data, 76, 80);
            System.out.println("avoid_state=" + ByteUtils.byte4ToInt(avoid_state, 0));
            byte[] avoid_finish_flag = Arrays.copyOfRange(data, 80, 81);
            System.out.println("avoid_finish_flag=" + avoid_finish_flag[0]);
            byte[] battery = Arrays.copyOfRange(data, 84, 88);
            System.out.println("battery=" + ByteUtils.byte4ToInt(battery, 0));
            System.out.println("解析完成");
        } catch (Exception e) {
            logger.info(e.getMessage());
            LogUtil.dumpException(e);
        }
    }
}
